var html_editors = [];
var auto_editors = {
    init: function() {
	    this.editors = [];                                                                                                                                     
	    var textareas = YAHOO.util.Dom.getElementsByClassName('simple-html-editor', 'textarea');
	    for(var i=0, item; item=textareas[i]; i++) {
	        // Create button to toggle editor
	        YAHOO.util.Dom.addClass(document.body, "yui-skin-sam");
	        var btn = document.createElement('button');
            btn.setAttribute('type', "button");
            btn.setAttribute('id', item.id+'-btn');
            btn.innerHTML = 'Toggle Editor';
            YAHOO.util.Dom.insertBefore(btn, item);
            YAHOO.util.Dom.insertAfter(document.createElement('br'), btn);
            
	        
	        this.editors[i] = new HTMLEditor(item);
	        var found_form = false;
	        var parent = item;
	        while (found_form == false) {
	            parent = parent.parentNode;
	            if (!parent) {
	                // No more parents so we've reached the top of the page
	                // just exit out of the loop.
	                found_form = true;
	            } else if (parent.nodeName == 'FORM') {
	                parent.onsubmit=save_editors;
	                found_form = true;
	            }
	        }
	    }
	    html_editors = this.editors;
    }
};

var save_editors = function() {
    for(var i=0, editor; editor=html_editors[i]; i++) {
        //If the editor is turned on then save the html and set it to the text area again
        // Incase there is any additional error checking
        if (editor.state == 'on') {
            editor.state = 'off';
            editor.editor.saveHTML();
            YAHOO.util.Dom.setStyle(editor.editor.get('element_cont').get('firstChild'), 'position', 'absolute');
            YAHOO.util.Dom.setStyle(editor.editor.get('element_cont').get('firstChild'), 'top', '-9999px');
            YAHOO.util.Dom.setStyle(editor.editor.get('element_cont').get('firstChild'), 'left', '-9999px');
            YAHOO.util.Dom.setStyle(editor.editor.get('textarea'), 'display', 'block');
        } 
    }
};
HTMLEditor = function(textarea) {
    if (textarea) {
        this.init(textarea); 
    }
};

HTMLEditor.prototype = {  
    init: function(textarea) {
        this.textarea = textarea;
        this.editor = new YAHOO.widget.Editor(textarea.id, { 
	        height: '300px', 
	        width: '540px', 
	        dompath: true, //Turns on the bar at the bottom 
	        animate: true, //Animates the opening, closing and moving of Editor windows 
            toolbar: {
                collapse: false,
                titlebar: false,
                draggable: false,
                buttons: [
                    { group: 'parastyle', label: 'Headings',
                        buttons: [
                        { type: 'select', label: 'Normal', value: 'heading', disabled: true,
                            menu: [
                                { text: 'Normal', value: 'none', checked: true },
                                { text: 'Header 1', value: 'h1' },
                                { text: 'Header 2', value: 'h2' },
                                { text: 'Header 3', value: 'h3' },
                                { text: 'Header 4', value: 'h4' },
                                { text: 'Header 5', value: 'h5' },
                                { text: 'Header 6', value: 'h6' }
                            ]
                        }
                        ]
                    },
                    { type: 'separator' },
                    { group: 'textstyle', label: 'Font Style',
                        buttons: [
                            { type: 'push', label: 'Bold CTRL + SHIFT + B', value: 'bold' },
                            { type: 'push', label: 'Italic CTRL + SHIFT + I', value: 'italic' },
                            { type: 'push', label: 'Underline CTRL + SHIFT + U', value: 'underline' },
                            { type: 'separator' },
                            { type: 'push', label: 'Subscript', value: 'subscript', disabled: true },
                            { type: 'push', label: 'Superscript', value: 'superscript', disabled: true },
                            { type: 'separator' },
                            { type: 'push', label: 'Remove Formatting', value: 'removeformat', disabled: true },
                            { type: 'push', label: 'Hidden Elements', value: 'hiddenelements' }
                        ]
                    },
                    { type: 'separator' },
                    { group: 'insertitem', label: 'Link',
                        buttons: [
                            { type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: true }
                        ]
                    },
                    { type: 'separator' },
                    { type: 'separator' },
                    { group: 'alignment', label: 'Alignment',
                        buttons: [
                            { type: 'push', label: 'Align Left CTRL + SHIFT + [', value: 'justifyleft' },
                            { type: 'push', label: 'Align Center CTRL + SHIFT + |', value: 'justifycenter' },
                            { type: 'push', label: 'Align Right CTRL + SHIFT + ]', value: 'justifyright' },
                            { type: 'push', label: 'Justify', value: 'justifyfull' }
                        ]
                    },
                    { type: 'separator' },
                    { group: 'indentlist', label: 'Indenting and Lists',
                        buttons: [
                            { type: 'push', label: 'Indent', value: 'indent', disabled: true },
                            { type: 'push', label: 'Outdent', value: 'outdent', disabled: true },
                            { type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' },
                            { type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' }
                        ]
                    }
                ]                

            }
	    }); 
	    this.state = 'not rendered';
	    
	    	    
        this.toggle_button = new YAHOO.widget.Button(this.textarea.id + '-btn');
        this.toggle_button.addClass('toggleEditor');
        var obj = this;

        this.toggle_button.on('click', function(e, obj) {
            YAHOO.util.Event.preventDefault(e);
            if (obj.state == 'not rendered'){
                obj.state = 'on';
                if (obj.textarea.value.indexOf('<br>') >= 0 || obj.textarea.value.indexOf('<br />') >= 0 || obj.textarea.value.indexOf('<p>') >= 0) {
                    obj.textarea.value = obj.textarea.value;
                } else {
                    obj.textarea.value = obj.textarea.value.replace(/\r\n|\n\r|\r|\n/g, '<br>');
                }
                obj.editor.render();
            } else if (obj.state == 'on') {
                obj.state = 'off';
                
                obj.editor.saveHTML();
                
                var new_value = obj.editor.get('textarea').value.replace(/\n/g, '');
                new_value = new_value.replace(/<br>/gi, '<br>\n');
                new_value = new_value.replace(/<br \/>/gi, '<br />\n');
                new_value = new_value.replace(/<p>/gi, '<p>\n');
                new_value = new_value.replace(/<\/p>/gi, '</p>\n');
                obj.editor.get('textarea').value = new_value;
                
                YAHOO.util.Dom.setStyle(obj.editor.get('element_cont').get('firstChild'), 'position', 'absolute');
                YAHOO.util.Dom.setStyle(obj.editor.get('element_cont').get('firstChild'), 'top', '-9999px');
                YAHOO.util.Dom.setStyle(obj.editor.get('element_cont').get('firstChild'), 'left', '-9999px');
                obj.editor.get('element_cont').removeClass('yui-editor-container');
                
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'visibility', 'visible');
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'top', '');
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'left', '');
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'position', 'static');
            } else {
                obj.state = 'on';

                YAHOO.util.Dom.setStyle(obj.editor.get('element_cont').get('firstChild'), 'position', 'static');
                YAHOO.util.Dom.setStyle(obj.editor.get('element_cont').get('firstChild'), 'top', '0');
                YAHOO.util.Dom.setStyle(obj.editor.get('element_cont').get('firstChild'), 'left', '0');
                
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'visibility', 'hidden');
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'top', '-9999px');
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'left', '-9999px');
                YAHOO.util.Dom.setStyle(obj.editor.get('element'), 'position', 'absolute');

                obj.editor.get('element_cont').addClass('yui-editor-container');
                obj.editor._setDesignMode('on');

                var new_value = obj.editor.get('textarea').value.replace(new RegExp("<br>[ \f\r\t\v\u00A0\u2028\u2029]*\n", "gi"), '<br>');
                new_value = new_value.replace(new RegExp("<br />[ \f\r\t\v\u00A0\u2028\u2029]*\n", "gi"), '<br />'); 
                new_value = new_value.replace(/<p>[ \f\r\t\v\u00A0\u2028\u2029]*\n/gi, '<p>'); 
                new_value = new_value.replace(new RegExp("</p>[ \f\r\t\v\u00A0\u2028\u2029]*\n", "gi"), '</p>');
                new_value = new_value.replace(/\n/g, '<br>'); 
                obj.editor.setEditorHTML(new_value);
            }
        }, obj);    
    }
};
var loader = new YAHOO.util.YUILoader({
    require: ["dom","event","element","editor"],
    loadOptional: true,
    onSuccess: auto_editors.init
});
loader.insert();